package edu.eas.auth.service.impl;

import edu.eas.auth.constant.MessageConstant;
import edu.eas.auth.domain.SecurityUser;
import edu.eas.auth.service.AdminService;
import edu.eas.auth.service.StudentService;
import edu.eas.auth.service.TeacherService;
import edu.eas.common.constant.AuthConstant;
import edu.eas.common.domain.UserDto;
import edu.eas.common.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理业务类
 *
 * @author cql
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private AdminService adminService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.获取 clientId 判断用户是从哪端进行登录的
        HttpServletRequest request = ServletUtils.getRequest();
        Integer userType = Integer.parseInt(request.getParameter(AuthConstant.USER_TYPE_PARAMETER_NAME));
        String clientId = request.getParameter(AuthConstant.CLIENT_ID_PARAMETER_NAME);

        // 2.根据不同的 clientId 去查询不同的用户信息
        UserDto userDto = new UserDto();
        if (AuthConstant.USER_TYPE_ADMIN.equals(userType)) {
            userDto = adminService.loadUserByUsername(username);
        } else if (AuthConstant.USER_TYPE_STUDENT.equals(userType)) {
            userDto = studentService.loadUserByUsername(username);
        } else if (AuthConstant.USER_TYPE_TEACHER.equals(userType)) {
            userDto = teacherService.loadUserByUsername(username);
        }
        if (userDto == null) {
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        userDto.setClientId(clientId);
        userDto.setUserType(userType);

        SecurityUser securityUser = new SecurityUser(userDto);

        // 3.判断账号是否可用等
        if (!securityUser.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!securityUser.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!securityUser.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!securityUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        return securityUser;
    }


}
